<script setup lang="ts">
import KnowledgeCard from './KnowledgeCard.vue'
import { getKnowledgePage } from '@/api/consult'
import type { KnowledgeType, KnowledgeList, KnowledgeParams } from '@/types/consult'
import { ref } from 'vue'
const props = defineProps<{
  type: KnowledgeType
}>()
const list = ref<KnowledgeList>([])
// 请求的分页参数
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})
const loading = ref(false)
const finished = ref(false)

const onLoad = async () => {
  // 异步更新数据
  // setTimeout 仅做示例，真实场景中一般为 ajax 请求
  const { data: res } = await getKnowledgePage(params.value)
  console.log('=list', res.data)

  list.value.push(...res.rows)
  console.log('=list', list)

  loading.value = false
  if (list.value.length === res.total) {
    finished.value = true
  } else {
    params.value.current++
  }
  // 加载状态结束
  loading.value = false

  // 数据全部加载完成
  if (list.value.length >= 40) {
    finished.value = true
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- knowledge-card 模版组件(父) :item="item"-->
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
